package com.fxnotes;

import com.fxnotes.common.ui.CustomNode;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Map;
import javafx.beans.property.Property;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

@Singleton
public class MainDialog implements CustomNode<Parent> {

    private Pane root;
    private TabPane tabPane;

    @Inject
    public MainDialog() {
        initUI();
    }

    private void initUI() {
        root = new StackPane();

        tabPane = new TabPane();
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        root.getChildren().add(tabPane);
    }

    public void addTabs(Map<String, Property<? extends Node>> modules) {
        for (Map.Entry<String, Property<? extends Node>> entry : modules.entrySet()) {
            String name = entry.getKey();
            Property<? extends Node> property = entry.getValue();

            Tab tab = new Tab();
            tab.setId(name);
            tab.setText(name);
            tab.contentProperty().bind(property);
            tabPane.getTabs().add(tab);
        }
    }

    public void selectTab(String name) {
        for (Tab tab : tabPane.getTabs()) {
            if (tab.getId().equals(name)) {
                tabPane.getSelectionModel().select(tab);
                return;
            }
        }
    }

    @Override
    public Parent getNode() {
        return root;
    }
}
